<%@ page import="java.net.ServerSocket" %><%--
  Created by IntelliJ IDEA.
  User: 25387
  Date: 2020/8/16
  Time: 13:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<input type="text" placeholder="输入你的内容">
<button>发送请求</button>
<div></div>
<script>
    var input=document.querySelector('input');
    var button=document.querySelector('button');
    var div=document.querySelector('div');
    //演示websocket在浏览器端如何使用
    //1.创建websocket连接
    //参数1  websocket的服务地址
  var socket=new WebSocket('ws://echo.websocket.org');
  //open:当和websocket服务连接成功的时候触发
  socket.addEventListener('open',function () {
    div.innerHTML='连接服务成功了';
  })
    //3.主动给websocket服务发送消息
    button.addEventListener('click',function () {
        var val=input.value
        socket.send(val)
    })
    //4.接收websocket服务端的数据
    socket.addEventListener('message',function (e) {
      console.log(e.data)
        div.innerHTML=e.data
    })

    socket.addEventListener('close',function () {
        div.innerHTML='服务断开连接'
    })
</script>
</body>
</html>
